package com.practice.java8.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class LimitSkipMain {
	
	public static void main(String[] args) {
		limitSkip();
		limitSkipBeforeSorted();
		limitSkipAfterSorted();
	}
	
	public static void limitSkip() {
		List<Person> numbers = new ArrayList<>();
		for (int i = 0; i < 10000; i++) {
			numbers.add(new Person(i));
		}
		
		List<Integer> result = numbers.stream().
			map(Person::getNumber).limit(10).skip(3).collect(Collectors.toList());
		System.out.println(result);
	}
	
	public static void limitSkipBeforeSorted() {
		List<Person> numbers = new ArrayList<>();
		for (int i = 0; i < 20; i++) {
			numbers.add(new Person(i));
		}
		
		List<Integer> result = numbers.stream().
			map(Person::getNumber).
			sorted(Integer::compareTo).
			limit(10).skip(3).
			collect(Collectors.toList());
		System.out.println(result);
	}
	
	public static void limitSkipAfterSorted() {
		List<Person> numbers = new ArrayList<>();
		for (int i = 0; i < 20; i++) {
			numbers.add(new Person(i));
		}
		
		List<Integer> result = numbers.stream().
			map(Person::getNumber).
			limit(10).skip(3).
			sorted(Integer::compareTo).
			collect(Collectors.toList());
		System.out.println(result);
	}

	private static class Person {
		
		private Integer number;

		public Person(Integer number) {
			this.number = number;
		}
		
		public Integer getNumber() {
			System.out.println(number);
			return number;
		}
		
	}
		
}
